//
//  XGL_ReportReasonViewController.swift
//  XGamesLive
//
//  Created by ios on 2024/5/23.
//

import UIKit
import CollectionKit

class XGL_ReportReasonViewController: UIViewController,CustomNavgationBar{
    struct ReasonData {
        var title: String = ""
        var isSelected: Bool = false
    }
    lazy var hudView: XGL_CustomHudView = XGL_CustomHudView()
    var collectionView: CollectionView!
    var datas: ArrayDataSource<ReasonData> = ArrayDataSource(data: [])
    
    var id: String = ""

    override func viewDidLoad() {
        super.viewDidLoad()
        self.createCustomBackgroundView()
        self.createXglView()
        self.creatClearNavigationBar(title: "")
        self.datas.data = [ReasonData(title: "Violation of laws and regulations"),ReasonData(title: "Pornographic vulgar"),ReasonData(title: "Scam information"),ReasonData(title: "Bootstrap transaction"),ReasonData(title: "Pretend to be someone else"),ReasonData(title: "Harass me"),ReasonData(title: "Other")]
        self.setCollectionData()
    }

}

extension XGL_ReportReasonViewController{
    @objc private func submitAction(){
        guard let selectReason = self.datas.data.first(where: {$0.isSelected}) else { return  }
        self.reportRequest(reason: selectReason.title)
    }
    
    @objc private func selectReportReasonAction(sender: UIButton){
        let index = sender.tag - 1600
        var datas = self.datas.data.map({ReasonData(title: $0.title)})
        datas[index].isSelected = true
        self.datas.data = datas
    }
}

extension XGL_ReportReasonViewController{
    func reportRequest(reason: String){

        self.hudView.hudView.backgroundColor = .clear
        self.hudView.hudView.startAnimation()
        self.view.addSubview(self.hudView)
        self.hudView.snp.makeConstraints { make in
            make.width.centerX.bottom.equalToSuperview()
            make.top.equalTo(PanNavibarH)
        }
        PanRequestManager<XGL_RequestBaseResponse<SendGiftResult>,XGL_CustomRequestError>().request(XGLPersonalRequestAPI(requestAPI: .ReportTreeData(id: self.id, reason: reason))) { result in
            self.hudView.dismiss()
            switch result{
            case .success(let response):
                MXProgressHUD.hudWithText("Submit successfully") {
                    self.navigationController?.popViewController()
                }
                break
            default:
                break
            }
        }
    }
}

extension XGL_ReportReasonViewController{
    private func createXglView(){
        let titleText = UILabel().then { make in
            make.textColor = UIColor.white
            make.font = UIFont.pingfangSC_Semibold(fontSize: 32.0)
            make.text = "Report"
        }
        let subtitleText = UILabel().then { make in
            make.textColor = UIColor.hexColor("#A2A2A2")
            make.font = UIFont.pingfangSC_Regular(fontSize: 14.0)
            make.text = "Please select the reason for reporting"
        }
        self.collectionView = CollectionView()
        let submitButton = UIButton().then { make in
            make.setTitle("Submit", for: .normal)
            make.setTitleColor(UIColor.hexColor("#191A1D"), for: .normal)
            make.titleLabel?.font = UIFont.pingfangSC_Regular(fontSize: 18.0)
            make.backgroundColor = UIColor.hexColor("#80FFE6")
            make.layerCornerRadius = 24.0
            make.addTarget(self, action: #selector(self.submitAction), for: .touchUpInside)
        }
        self.view.addSubviews([titleText,subtitleText,self.collectionView,submitButton])
        titleText.snp.makeConstraints { make in
            make.left.equalTo(15.0)
            make.top.equalTo(PanNavibarH + 12.0)
        }
        subtitleText.snp.makeConstraints { make in
            make.left.equalTo(15.0)
            make.top.equalTo(titleText.snp.bottom).offset(17.0)
        }
        self.collectionView.snp.makeConstraints { make in
            make.centerX.width.equalToSuperview()
            make.top.equalTo(subtitleText.snp.bottom).offset(10.0)
            make.bottom.equalTo(submitButton.snp.top).offset(-10.0)
        }
        submitButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.height.equalTo(48.0)
            make.width.equalTo(conversionWithScrennWidth(value: 345.0))
            make.bottom.equalTo(-BottomHeight - 40.0)
        }
    }
    
    private func setCollectionData(){
        let viewSource = ClosureViewSource(viewUpdater: { [weak self] (view: UIView, data: ReasonData, index: Int) in
            guard let `self` = self else { return }
            view.backgroundColor = .clear
            view.removeSubviews()
            let itemContentView = UIView().then { make in
                make.backgroundColor = UIColor.hexColor("#001310").withAlphaComponent(0.5)
                make.layerCornerRadius = 12.0
            }
            let titleText = UILabel().then { make in
                make.text = data.title
                make.textColor = UIColor.white
                make.font = UIFont.pingfangSC_Regular(fontSize: 16.0)
            }
            let rightIconView = UIImageView().then { make in
                make.image = data.isSelected ? imageWithName("icon_xgl_circle_selected") : imageWithName("icon_xgl_circle_normal")
            }
            let button = UIButton().then { make in
                make.tag = index + 1600
                make.addTarget(self, action: #selector(self.selectReportReasonAction(sender:)), for: .touchUpInside)
            }
            itemContentView.addSubviews([titleText,rightIconView,button])
            view.addSubview(itemContentView)
            titleText.snp.makeConstraints { make in
                make.centerY.equalToSuperview()
                make.left.equalTo(15.0)
            }
            rightIconView.snp.makeConstraints { make in
                make.centerY.equalToSuperview()
                make.right.equalTo(-15.0)
                make.width.height.equalTo(24.0)
            }
            button.snp.makeConstraints { make in
                make.edges.equalToSuperview()
            }
            itemContentView.snp.makeConstraints { make in
                make.left.equalTo(15.0)
                make.center.equalToSuperview()
                make.top.equalTo(10.0)
            }
        })
        let sizeSource = { (index: Int, data: ReasonData, collectionSize: CGSize) -> CGSize in
            return CGSize(width: collectionSize.width, height: 79.0)
        }
        let provider = BasicProvider(
            dataSource: self.datas,
            viewSource: viewSource,
            sizeSource: sizeSource
        )
        self.collectionView.provider = provider
    }
}
